package com.jiangyao.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ReflectUtils {

    public static void setValue(Object model, String setName, String setValue) {
        Field[] field = model.getClass().getDeclaredFields();
        try {
            for (int j = 0; j < field.length; j++) {
                String name = field[j].getName();
                if (name.equals(setName)) {
                    name = name.substring(0, 1).toUpperCase() + name.substring(1);
                    String type = field[j].getGenericType().toString();
                    if (type.equals("class java.lang.String")) {
                        Method m = model.getClass().getMethod("get" + name);
                        String value = (String) m.invoke(model);
                        if (value == null) {
                            m = model.getClass().getMethod("set" + name, String.class);
                            m.invoke(model, setValue);
                        }
                    }
                    if (type.equals("class java.lang.Integer")) {
                        Method m = model.getClass().getMethod("get" + name);
                        Integer value = (Integer) m.invoke(model);
                        if (value == null) {
                            m = model.getClass().getMethod("set" + name, Integer.class);
                            m.invoke(model, Integer.parseInt(setValue));
                        }
                    }
                    if (type.equals("class java.lang.Long")) {
                        Method m = model.getClass().getMethod("get" + name);
                        Long value = (Long) m.invoke(model);
                        if (value == null) {
                            m = model.getClass().getMethod("set" + name, Long.class);
                            m.invoke(model, Long.parseLong(setValue));
                        }
                    }
                    if (type.equals("class java.lang.Boolean")) {
                        Method m = model.getClass().getMethod("get" + name);
                        Boolean value = (Boolean) m.invoke(model);
                        if (value == null) {
                            m = model.getClass().getMethod("set" + name, Boolean.class);
                            m.invoke(model, Boolean.parseBoolean(setValue));
                        }
                    }
                    if (type.equals("class java.util.Date")) {
                        Method m = model.getClass().getMethod("get" + name);
                        Date value = (Date) m.invoke(model);
                        if (value == null) {
                            m = model.getClass().getMethod("set" + name, Date.class);
//                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            SimpleDateFormat sfStart = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", java.util.Locale.ENGLISH);
                            m.invoke(model, sfStart.parse(setValue));
                        }
                    }
                    break;
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static Object getValueFormObject(Object object, String fieldName) {
        if (object == null) {
            return null;
        }
        if (fieldName == null || fieldName == "") {
            return null;
        }
        Field field;
        try {
            field = object.getClass().getDeclaredField(fieldName);
            if (field != null) {
                field.setAccessible(true);
                return field.get(object);
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }

        return null;
    }
}
